package com.ccys.common.third.sms;


import com.alibaba.fastjson.JSONObject;
import com.ccys.common.util.RestUtil;
import com.ccys.common.util.StringUtil;
import com.nexmo.client.NexmoClient;
import com.nexmo.client.NexmoClientException;
import com.nexmo.client.sms.MessageStatus;
import com.nexmo.client.sms.SmsSubmissionResponse;
import com.nexmo.client.sms.messages.TextMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Demo 短信
 * @author Administrator
 *
 * 参考地址 ：https://sg.smsdome.com/
 */
@Slf4j
@Component
@SuppressWarnings("unchecked")
@DependsOn("smsConstants")
public class DomeSms {

    private static String NEXMO_URL = SmsConstants.NEXMO_URL;
    private static String SENDER_NAME = SmsConstants.SENDER_NAME;
    private static String NEXMO_API_KEY = SmsConstants.NEXMO_API_KEY;
    private static String NEXMO_API_SECRET = SmsConstants.NEXMO_API_SECRET;

    static {
        if(StringUtil.isEmpty(NEXMO_URL)) {
            DomeSms.NEXMO_URL = "https://rest.nexmo.com/sms/json";
        }
        if(StringUtil.isEmpty(SENDER_NAME)) {
            DomeSms.SENDER_NAME = "WhiteCoat";
        }
        if(StringUtil.isEmpty(NEXMO_API_KEY)) {
            DomeSms.NEXMO_API_KEY = "9c267750";
        }
        if(StringUtil.isEmpty(NEXMO_API_SECRET)) {
            DomeSms.NEXMO_API_SECRET = "H5CqODqOrLRzLtA3";
        }

    }

    private DomeSms(){

    }
    public static void main(String[] args) {
        String phone = "+8618408279830";
        String content = "Hello from Nexmo Java";
        sendSmsContent(phone,content,"86");
    }

       /**
        * @description 发送 DemoSms 短信 (确认需求 该短信发送废弃，请使用nexmo 发送)
        * @param phone 电话号码
        * @param dial 区号
        * @param content 内容
        * @date     2024/1/30
        * @author   cloud fly
        */
    @Deprecated
    public static boolean sendSmsContent(String phone,String content,String dial) {
        phone = "+" + dial + phone;
        boolean flag = false;
        try {
            // 构建请求URL
            String apiKey = "your_api_key";
            String apiSecret = "your_api_secret";
            String url = "http://www.smsdome.com/api/http/sendsms.aspx";
            Map<String, String> headerMap = new HashMap<>();
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("appid", URLEncoder.encode(apiKey, "UTF-8"));
            paramMap.put("appsecret", URLEncoder.encode(apiSecret, "UTF-8"));
            paramMap.put("receivers", URLEncoder.encode(phone, "UTF-8"));
            paramMap.put("content", URLEncoder.encode(content, "UTF-8"));
            paramMap.put("responseformat", "JSON");
            paramMap.put("appid", URLEncoder.encode(apiKey, "UTF-8"));
            String s = RestUtil.executeForm(url, HttpMethod.GET, headerMap, paramMap);
            System.out.println("result:" + s);
            flag = true;
            // 获取响应
            JSONObject jsonObject = JSONObject.parseObject(s);
            /*if (jsonObject != null) {
                // 处理响应
                System.out.println("短信已成功发送");
                System.out.println("响应内容：" + response.toString());
            } else {
                System.out.println("短信发送失败，错误码：" + responseCode);
            }*/

        } catch (Exception e) {
            System.err.println("短信发送失败: " + e.getMessage());
        }
        return flag;
    }


}
